package com.hbx.educenter.controller;


import com.hbx.commonutils.JwtUtils;
import com.hbx.commonutils.R;
import com.hbx.commonutils.ordervo.UcenterMemberOrder;
import com.hbx.educenter.entity.UcenterMember;
import com.hbx.educenter.entity.vo.RegisterVo;
import com.hbx.educenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author hbx
 * @since 2023-10-15
 */
@Api(tags = {"登陆注册管理"})
@RestController
@CrossOrigin
@RequestMapping("/educenter/member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    //登录
    @PostMapping("/login")
    public R login(@RequestBody UcenterMember ucenterMember){
        //返回token值，使用jwt生成
        String token = memberService.login(ucenterMember);
        return R.ok().data("token",token);
    }

    //注册
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo register){
        memberService.register(register);
        return R.ok();
    }

    //根据token得到member信息
    @GetMapping("/getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用jwt工具类，根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("member",member);
    }

    //根据用户id返回用户信息，通过远程调用，用于创建订单
    @PostMapping("/getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){
        UcenterMember member = memberService.getById(id);
        UcenterMemberOrder order = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,order);
        return order;
    }

    //查询某一天的注册人数
    @GetMapping("/countRegister/{day}")
    public R countRegister(@PathVariable String day){
        Integer count = memberService.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }
}

